/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trampa;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;

/**
 *
 * @author david
 */
public class DragImage {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Drag Image");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon icon = new ImageIcon("./airhockey32.gif");
    Icon icon2 = new ImageIcon("./rojo.gif");
    JPanel jsp = null;
    JLabel[] labels = createLabels();

    for (int i=0;i<labels.length;i++){
        labels[i] = new JLabel(icon);
        labels[i].setTransferHandler(new ImageSelection());
        MouseListener listener = new MouseAdapter() {
          public void mousePressed(MouseEvent me) {
            JComponent comp = (JComponent) me.getSource();
            TransferHandler handler = comp.getTransferHandler();
            handler.exportAsDrag(comp, me, TransferHandler.COPY);
          }
        };
        labels[i].addMouseListener(listener);
        if ( jsp == null ){
           jsp = new JPanel(new GridLayout(6, 5));
        }
        jsp.add(labels[i]);
    }
//    frame.add(jsp, BorderLayout.CENTER);
//    JLabel label = new JLabel(icon);
      JLabel label2 = new JLabel(icon2);
//    label.setTransferHandler(new ImageSelection());
      label2.setTransferHandler(new ImageSelection());
//      MouseListener listener = new MouseAdapter() {
//      public void mousePressed(MouseEvent me) {
//        JComponent comp = (JComponent) me.getSource();
//        TransferHandler handler = comp.getTransferHandler();
//        handler.exportAsDrag(comp, me, TransferHandler.COPY);
//      }
//      };
        MouseListener listener2 = new MouseAdapter() {
        public void mousePressed(MouseEvent me) {
          JComponent comp = (JComponent) me.getSource();
          TransferHandler handler = comp.getTransferHandler();
          handler.exportAsDrag(comp, me, TransferHandler.COPY);
        }
      };

      label2.addMouseListener(listener2);
      JLabel[] labels15 = createLabels();
//    jsp.add(label);
//    //jsp.add(label);
      jsp.setPreferredSize( new Dimension(100, 100));
    //  frame.add(jsp, BorderLayout.CENTER);
//      Frame frame2= new Frame();
//      frame2.setSize(100, 100);
//      frame2.add(jsp,BorderLayout.NORTH);
      frame.add(jsp,BorderLayout.CENTER);
      JPanel jp15 = new JPanel(new GridLayout(16,1));
      jp15.getSize();
      for( int i=0;i<10;i++){
          labels15[i].setTransferHandler(new ImageSelection());
          labels15[i].setIcon(new ImageIcon("./"+i+".gif"));
          if ( new ImageIcon("./"+i+".gif") != null){
                  MouseListener listener3 = new MouseAdapter() {
                public void mousePressed(MouseEvent me) {
                  JComponent comp = (JComponent) me.getSource();
                  TransferHandler handler = comp.getTransferHandler();
                  handler.exportAsDrag(comp, me, TransferHandler.COPY);
                }
              };
               labels15[i].addMouseListener(listener3);
               jp15.add( labels15[i]);
          }
      }
      
      frame.add(jp15, BorderLayout.EAST
              );
//
      frame.setSize(400, 900);
      frame.setVisible(true);
  }
    public static JLabel[] createLabels(){
        JLabel[] labels=new JLabel[30];
        for (int i=0;i<10;i++){
            labels[i]=new JLabel("message" + i);
        }
        return labels;
    }
}